/**
 * <p>Title: Record.java</p>
 * <p>Description: CS 343 Final Project. This class is dealing with the record of scores</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Components;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Record implements Comparable<Record> {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String username;
	private int score;
	private int mph;

	@Temporal(value = TemporalType.DATE)
	private Date time;

	/**
	 * @param user
	 * @param gameScore
	 * @param topSpeed
	 * @description creates a record for the user and their score and top speed
	 */
	public Record(String user, int gameScore, int topSpeed) {
		username = user;
		score = gameScore;
		mph = topSpeed;
		time = new Date();
	}

	/**
	 * @param none
	 * @description creates default record
	 */
	public Record() {
		username = "";
		score = 0;
		mph = 0;
		time = new Date();
	}

	/**
	 * @param none
	 * @description gets the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 * @description sets the username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @param none
	 * @description gets the score
	 */
	public int getScore() {
		return score;
	}

	/**
	 * @param score
	 * @description sets the score
	 */
	public void setScore(int score) {
		this.score = score;
	}

	/**
	 * @param none
	 * @description gets the MPH
	 */
	public int getMph() {
		return mph;
	}

	/**
	 * @param mph
	 * @description sets the mph
	 */
	public void setMph(int mph) {
		this.mph = mph;
	}

	/**
	 * @param none
	 * @description gets the time
	 */
	public Date getTime() {
		return time;
	}

	/**
	 * @param time
	 * @description sets the time
	 */
	public void setTime(Date time) {
		this.time = time;
	}

	@Override
	/**
	 * @param none
	 * @description to string for the records
	 */
	public String toString() {
		String tempUser = username;
		if (tempUser.length() > 20) {
			tempUser = tempUser.substring(0, 17);
			tempUser = tempUser + "...";
		}
		String x = (String.format("%-20s  %8d  %4d", tempUser, score, mph));
		System.out.println(x);
		return x;
	}

	@Override
	/**
	 * @param record
	 * @description compares the records to sort them
	 */
	public int compareTo(Record record) {
		if (record.getScore() > this.score)
			return 1;
		if (record.getScore() == this.score) {
			if ( record.getTime().getTime() < this.time.getTime())
				return 1;
			return -1;
		}
		return -1;
	}

}
